/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseserver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 *
 * @author ntfsbk
 */
public class CheckOnlineThread extends Thread {

    private int timeInterval;
    private boolean stop = false;

    public CheckOnlineThread() {
    }

    public CheckOnlineThread(int TimerInterval) {
        this.timeInterval = TimerInterval;
    }

    public void setStop() {
        this.stop = true;
    }

    public boolean isStop() {
        return stop;
    }

    @Override
    public void run() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost:1433;"
                    + "databaseName=sharing;userName=sa;password=123456";

            while (true) {
                if (stop == false) {
                    Connection conn = DriverManager.getConnection(connectionUrl);
                    Statement statement = conn.createStatement();
                    String sqlQuery = "update users set available=0 where DATEDIFF(ms,lastRequest,GETDATE())> " + timeInterval;
                    statement.executeUpdate(sqlQuery);
                    conn.close();
                    Thread.currentThread().sleep(timeInterval);
                }
                else
                    return;

            }
        } catch (Exception e) {
        }
    }
}
